package com.soecode.lyf.web;

import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.soecode.lyf.dto.Result;
import com.soecode.lyf.entity.Comment;
import com.soecode.lyf.entity.User;
import com.soecode.lyf.entity.UserInfo;
import com.soecode.lyf.service.FocusService;

@Controller
//@Scope("session") // 表示会话级别
@RequestMapping("/focus") // url:/模块/资源/{id}/细分 /seckill/list
public class FocusController {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private FocusService focusservice;
	
	@ResponseBody
	@RequestMapping(value = "focuson", method = RequestMethod.GET)
	public Result Focuson(String focusid, String userid, Model model) {
		Result result = null;
		System.out.println("1"+focusid+"&&&&"+userid);
		if(!StringUtils.isEmpty(focusid)&&!StringUtils.isEmpty(userid) && !focusid.equals(userid)){
			int i = focusservice.FocusOn(userid, focusid);
			if(i==0) {
			result = new Result(1,"关注失败");		
			}else{
			result = new Result(0,"关注成功");	
			}
		}else if(focusid.equals(userid)) {
			result = new Result(1,"不能关注自己");	
		}
		return result;
	};
	
	@ResponseBody
	@RequestMapping(value = "returnfocus", method = RequestMethod.GET)
	public Result returnFocuson(String userid, Model model) {
		Result result = null;
		if(!StringUtils.isEmpty(userid)){
			List<User> list = focusservice.returnFocus(userid);
			model.addAttribute("FOCUSLIST",list);
			result = new Result(0,model,"查询成功");		
		}
		return result;
	};
	@ResponseBody
	@RequestMapping(value = "adopt", method = RequestMethod.GET)
	public Result adopt(String userid,String questionid, String adoptid, Model model) {
		Result result = null;
		if(!StringUtils.isEmpty(userid)&&!StringUtils.isEmpty(questionid)&&!StringUtils.isEmpty(adoptid)){
			int adopt = focusservice.QuestionAdopt(userid,questionid,adoptid);
			if(adopt!=0) {
				int i = focusservice.addIntegral(userid);
				if(i!=0) {
					result = new Result(0,"采纳成功");
				}
			}		
		}
		return result;
	};
	
	@ResponseBody
	@RequestMapping(value = "returnAllfocus", method = RequestMethod.GET)
	public Result returnAllFocus(String userid,Model model,int page,int size) {
		Result result = null;
		if(!StringUtils.isEmpty(userid)){
			PageHelper.startPage(page,size);
			List<User> list = focusservice.AllFocus(userid);		
			PageInfo<User> page1 = new PageInfo<User>(list);
			model.addAttribute("FOCUSLIST",list);
	        model.addAttribute("PAGESUM",page1.getTotal());
			result = new Result(0,model,"查询分页成功");	
		}
		return result;
	};
	
	@ResponseBody
	@RequestMapping(value = "cancelfocus", method = RequestMethod.GET)
	public Result cancelFocus(String userid,Model model,String focusid) {
		Result result = null;
		if(!StringUtils.isEmpty(userid)&&!StringUtils.isEmpty(focusid)){
			int i = focusservice.cancelFocus(userid,focusid);	
			if(i!=0) {
				result = new Result(0,"取消关注成功");		
			}
		}
		return result;
	};
}
